Static Method in Java With Examples 您所在的位置:网站首页 Static properties and methods Static Method in Java With Examples

Static Method in Java With Examples

2024-05-19 01:02| 来源: 网络整理| 查看: 265

The static keyword is used to construct methods that will exist regardless of whether or not any instances of the class are generated. Any method that uses the static keyword is referred to as a static method.

Features of static method:

A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class’s object (instance). Only static data may be accessed by a static method. It is unable to access data that is not static (instance variables). In both static and non-static methods, static methods can be accessed directly.

Syntax to declare the static method:

Access_modifier static void methodName() { // Method body. }

The name of the class can be used to invoke or access static methods.

Syntax to call a static method:

className.methodName(); Example 1: The static method does not have access to the instance variable

The JVM runs the static method first, followed by the creation of class instances. Because no objects are accessible when the static method is used. A static method does not have access to instance variables. As a result, a static method can’t access a class’s instance variable.

Java

// Java program to demonstrate that // The static method does not have // access to the instance variable    import java.io.*;    public class GFG {     // static variable     static int a = 40;        // instance variable     int b = 50;        void simpleDisplay()     {         System.out.println(a);         System.out.println(b);     }        // Declaration of a static method.     static void staticDisplay()     {        System.out.println(a);      }        // main method     public static void main(String[] args)     {         GFG obj = new GFG();         obj.simpleDisplay();            // Calling static method.         staticDisplay();     } } Output 40 50 40 Example 2: In both static and non-static methods, static methods are directly accessed. Java

// Java program to demonstrate that // In both static and non-static methods, // static methods are directly accessed.    import java.io.*;    public class StaticExample {          static int num = 100;     static String str = "GeeksForGeeks";        // This is Static method     static void display()     {         System.out.println("static number is " + num);         System.out.println("static string is " + str);     }        // non-static method     void nonstatic()     {         // our static method can accessed          // in non static method         display();     }        // main method     public static void main(String args[])     {         StaticExample obj = new StaticExample();                  // This is object to call non static function         obj.nonstatic();                  // static method can called          // directly without an object         display();     } } Output static number is 100 static string is GeeksForGeeks static number is 100 static string is GeeksForGeeks Why use Static Methods? To access and change static variables and other non-object-based static methods. Utility and assist classes frequently employ static methods. Restrictions in Static Methods: Non-static data members or non-static methods cannot be used by static methods, and static methods cannot call non-static methods directly. In a static environment, this and super aren’t allowed to be used. Why is the main method in Java static?

It’s because calling a static method isn’t needed of the object. If it were a non-static function, JVM would first build an object before calling the main() method, resulting in an extra memory allocation difficulty.

Difference Between the static method and instance method

Instance Methods

Static Methods

It requires an object of the class. It doesn’t require an object of the class. It can access all attributes of a class. It can access only the static attribute of a class. The methods can be accessed only using object reference. The method is only accessed by class name. Syntax: Objref.methodname() Syntax: className.methodname() It’s an example of pass-by-value programming. It is an example of pass-by-reference programming. Like Article Suggest improvement Next Field set() method in Java with Examples Share your thoughts in the comments Please Login to comment...


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有